<template>
  <div class="login-wrap">
    <div class="login-root">
      <div class="login-main">
        <div class="login-container">
          <div class="logo"><img src="../../assets/logo.png" height="67" width="200" /></div>
          <lay-form  :model="loginForm" required>
            <lay-form-item :label-width="0">
              <lay-input placeholder="用户名" v-model="loginForm.Username" prefix-icon="layui-icon-user" size="lg">
              </lay-input>
            </lay-form-item>
            <lay-form-item :label-width="0">
              <lay-input placeholder="密码" password type="password" v-model="loginForm.Password"
                prefix-icon="layui-icon-password" size="lg"></lay-input>
            </lay-form-item>
            <lay-form-item :label-width="0">
                <lay-checkbox name="like" v-model="loginForm.Remember" skin="primary" :value="true">记住密码(7天持久)</lay-checkbox>
            </lay-form-item>
            <lay-form-item :label-width="0">
              <lay-button type="primary"  fluid="true" @click.prevent="loginSubmit()"  native-type="loginSubmit">登录</lay-button>
            </lay-form-item>
          </lay-form>
          <lay-line> </lay-line>
        </div>
      </div>
    </div>    
  </div>
</template>
<script lang="ts" setup>
import { login, userInfo } from '../../api/module/user/user';
import { reactive, ref } from "vue";
import { useRouter } from "vue-router";
import { useUserStore } from '../../store/user';
import { layer } from "@layui/layer-vue";
import { setToken } from '../../library/cookieUtil'
import { useAppStore } from "../../store/app";
import Cookies from 'js-cookie'
import { getNoticeCount } from "../../api/module/statistics";

const router = useRouter();
const userStore = useUserStore();

let loginForm = reactive({
  Username: "",
  Password: "",
  Remember: true
})
const appStore = useAppStore();

let username = Cookies.get('username')
let password = Cookies.get('password')
loginForm.Username = username === undefined ? loginForm.Username : username;
loginForm.Password = password === undefined ? loginForm.Password : password;

const loginSubmit = async () => {
  try {
    let { token, status, msg } = await login(loginForm);
    if (status == 1) {
      layer.msg(msg, { icon: 1 }, async () => {
        if (loginForm.Remember == true) {
          setToken(token, 7);//7天持久化
          Cookies.set('username', loginForm.Username, { expires: 30 })
          Cookies.set('password', loginForm.Password, { expires: 30 })
        } else {
          setToken(token, 1);//1天持久化
          Cookies.remove('username')
          Cookies.remove('password')
        }

        await userInfo().then((res) => {
          if (res.status == 1) {
            userStore.userInfo = res.data;
            userStore.systemInfo = res.sytemConfig;
            userStore.statistics = res.statistics;
            appStore.modChange = true;
          } else {
            layer.msg('获取系统及用户信息失败', { icon: 2 });
            return false;
          }
        });
        router.push('/');
      })
    } else {
      layer.msg(msg, { icon: 2 })
    }
  } catch (error) {
    console.log('er:', error)
  }

}

</script>

<style scoped>
.logo {
  margin: auto;
  text-align: center;
  height: 100px;
  width: 300px;
}

.login-one-ball {
  opacity: 0.4;
  position: absolute;
  max-width: 568px;
  left: -400px;
  bottom: 0px;
}

.login-two-ball {
  opacity: 0.4;
  position: absolute;
  max-width: 320px;
  right: -200px;
  top: -60px;
}

.login-wrap {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  overflow: auto;
  z-index: 9;
  background-image: url("../../assets/login/loginbg.jpg");
  background-repeat: no-repeat;
  background-size: cover;
  min-height: 100vh;
}

.login-wrap :deep(.layui-input-block) {
  margin-left: 0 !important;
}

.login-root {
  position: absolute;
  top: 40%;
  left: 50%;
  transform: translate(-50%, -50%);
  display: flex;
  justify-content: center;
  width: 100%;
  min-width: 320px;
  background-color: initial;
}

.login-main {
  position: relative;
  display: block;
}

.logo-container {
  max-width: calc(100vw - 28px);
  margin-bottom: 40px;
  text-align: center;
  display: flex;
  align-items: center;
  justify-content: center;
}

.logo-container .logo {
  display: inline-block;
  height: 30px;
  width: 143px;
  background: url() no-repeat 50%;
  background-size: contain;
  cursor: pointer;
}

.login-container {
  padding: 30px;
  position: relative;
  overflow: hidden;
  width: 400px;
  max-width: calc(100vw - 28px);
  border: 1.5px solid #fff;
  border-radius: 8px;
  background: hsla(0, 0%, 100%, 0.5);
  backdrop-filter: blur(30px);
}

.login-container .layui-tab-head {
  background: transparent;
}

.login-container .layui-input-wrapper {
  margin-top: 10px;
  margin-bottom: 10px;
}

.login-container .layui-input-wrapper {
  margin-top: 12px;
  margin-bottom: 12px;
}

.login-container .assist {
  margin-top: 5px;
  margin-bottom: 5px;
  letter-spacing: 2px;
}

.login-container .layui-btn {
  margin: 10px 0px 10px 0px;
  letter-spacing: 2px;
  height: 40px;
}

.login-container .layui-line-horizontal {
  letter-spacing: 2px;
  margin-bottom: 34px;
  margin-top: 24px;
}

.other-ways {
  display: flex;
  justify-content: space-between;
  margin: 0;
  padding: 0;
  list-style: none;
  font-size: 14px;
  font-weight: 400;
}

.other-ways li {
  width: 100%;
}

.line-container {
  justify-content: center;
  align-items: center;
  text-align: center;
  cursor: pointer;
}

.line-container .icon {
  height: 28px;
  width: 28px;
  margin-right: 0;
  vertical-align: middle;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 2px 0 rgb(9 30 66 / 4%), 0 1px 4px 0 rgb(9 30 66 / 10%), 0 0 1px 0 rgb(9 30 66 / 10%);
}

.line-container .text {
  display: block;
  margin: 12px 0 0;
  font-size: 12px;
  color: #8592a6;
}

:deep(.layui-tab-title .layui-this) {
  background-color: transparent;
}
</style>
